package com.quectel.core.module.pet.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.pet.dao.PetDao;
import com.quectel.core.module.pet.dto.PetDto;
import com.quectel.core.module.pet.entity.PetEntity;
import com.quectel.core.module.pet.service.PetService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.dto.VillageRoomDto;
import com.quectel.core.module.village.service.VillageRoomService;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/11/08 16:25
 */
@DubboService
public class PetServiceImpl extends ServiceImpl<PetDao, PetEntity> implements PetService{

    private static final Logger LOGGER = LoggerFactory.getLogger(PetServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PET_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PET_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<PetDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PET_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private PetDao petDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private VillageRoomService villageRoomService;


    @Override
    public void saveBatch(List<PetDto> list) {
        List<PetEntity> petEntities = CopyUtils.copyList(list, PetEntity.class);
        for (PetEntity petEntity : petEntities) {
            petEntity.setId(Snowflake.nextId());
        }
        super.saveBatch(petEntities);
    }

    @Override
    public PetDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PET_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PetDto selectById(Long id){
            PetEntity entity = petDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,PetDto.class));
    }

    @Override
    public List<PetDto> queryList(Map<String,Object> params){
        List<PetEntity> list = petDao.queryList(params);
        List<PetDto> result =  CopyUtils.copyList(list,PetDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<PetDto> queryListWithoutPadding(Map<String,Object> params){
        List<PetEntity> list = petDao.queryList(params);
        return CopyUtils.copyList(list,PetDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private PetDto paddingField(PetDto dto){
        if (dto != null){
            VillageDto villageDto = villageService.selectCacheById(dto.getVillageId());
            dto.setVillage(villageDto);

            VillageRoomDto villageRoomDto = villageRoomService.selectCacheById(dto.getRoomId());
            dto.setVillageRoom(villageRoomDto);
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return petDao.queryTotal(params);
    }

    @Override
    public Long save(PetDto dto){
        PetEntity entity = CopyUtils.copyObj(dto,PetEntity.class);
        entity.setId(Snowflake.nextId());
        petDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PetDto dto){
        PetEntity entity = CopyUtils.copyObj(dto,PetEntity.class);

        petDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(PetDto dto) {
       PetEntity entity = CopyUtils.copyObj(dto,PetEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, petDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        petDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
